<?php

include_once "../model/model.php";
include_once "../model/pile.php";
if (isset($_SESSION["admin"])) {
	header("location:../managerlogin.php");
}

if ($_GET["action"] === "del") {
	if (isset($_POST["name"])) {
	    $connect = new CONNECT();
	    $connect = $connect->connecttodb();
	    $action = $connect->prepare("DELETE FROM `admin` WHERE name = ?");
	    $result = $action->execute(array($_POST["name"]));
	    if ($result) {
	        header("location:../admin/addmanager.php");
	    }
	}
}elseif ($_GET["action"] === "reset") {
	if (!isset($_POST["name"]) || !isset($_POST["password"]) || !isset($_POST["configure"]) || $_POST["name"] === "") {
		echo "<script>alert('系统错误!');</script>";
	}elseif ($_POST["password"] === "" || $_POST["configure"] === "") {
		echo "<script>alert('信息不完整!')</script>";
	}elseif ($_POST["password"] !== $_POST["configure"]) {
		echo "<script>alert('两次密码不一致!')</script>";
	}else{
		$connect = new CONNECT();
		$connect = $connect -> connecttodb();
		$action = $connect -> prepare("UPDATE admin SET password = ? WHERE name = ?");
		$result = $action -> execute(array(md5($_POST["password"]), $_POST["name"]));
		if(!$result){
			echo "<script>alert('系统错误(arg.name error!)!');</script>";
		}
	}
	echo "<script>location.href='../admin/addmanager.php'</script>";
}
?>